package com.example.home.volunteer_service.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SearchView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.tool.OKHttpTool;
import com.example.home.volunteer_service.adapter.VolunteerListAdapter;
import com.example.home.volunteer_service.bean.ApplyBean;
import com.example.home.volunteer_service.bean.VolunteerListBean;

import org.json.JSONException;
import org.json.JSONObject;


public class MeActivityFragment extends Fragment {

    private RelativeLayout qeq;
    private ImageView back;
    private SearchView searchView;
    private RecyclerView rcvList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me_activity, container, false);


        back = view.findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        searchView = view.findViewById(R.id.searchView);
        rcvList = view.findViewById(R.id.rcv_list);

        getListData();
        return view;
    }

    private void getListData() {
        OKHttpTool.doNetRequest("prod-api/api/volunteer-service/activity/list",
                OKHttpTool.GET,
                VolunteerListBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        VolunteerListBean volunteerListBean = (VolunteerListBean) obj;
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindListData(volunteerListBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindListData(VolunteerListBean volunteerListBean) {
        VolunteerListAdapter serviceNewsAdapter = new VolunteerListAdapter(getActivity(), volunteerListBean.getRows(), new VolunteerListAdapter.VolunCallBack() {
            @Override
            public void CallBack(int position) {
                Integer id = volunteerListBean.getRows().get(position).getId();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("activityId",id);
                    jsonObject.put("newState","true");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                OKHttpTool.doNetRequest("prod-api/api/volunteer-service/register",
                        OKHttpTool.POST,
                        ApplyBean.class,
                        new OKHttpTool.NetCallback() {
                            @Override
                            public void onSuccess(Object obj) {
                                ApplyBean applyBean = (ApplyBean) obj;
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (applyBean.getCode() == 200){
                                            Toast.makeText(getActivity(),"报名成功",Toast.LENGTH_SHORT).show();
                                        }else if (applyBean.getCode() == 500){
                                            Toast.makeText(getActivity(),"当前活动已无法进行报名相关操作",Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }

                            @Override
                            public void onFailure(String msg) {

                            }
                        },jsonObject.toString());
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        rcvList.setAdapter(serviceNewsAdapter);
        rcvList.setLayoutManager(linearLayoutManager);
    }
}